<?php

namespace App\Jobs;

use App\Models\StationBills;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

class CreateStationBill implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $start_day;
    public $id;
    public $now_day;
    public $bill;

    /**
     *
     * CreateStationBill constructor.
     * @param $start_day
     * @param $id
     * @param $now_day
     * @param $bill
     */
    public function __construct( $start_day, $id, $now_day, $bill)
    {
        //
        $this->start_day = $start_day;
        $this->id = $id;
        $this->now_day = $now_day;
        $this->bill = $bill;
    }


    public function handle()
    {
        $log = new Logger('register');
        try {
            StationBills::query()
                ->updateOrCreate(
                    ['start_time' => $this->start_day, 'service_id' => $this->id],
                    ['end_time' => $this->now_day, 'total_sign' => $this->bill['sign'], 'charge' => '0.1', 'total_income' => $this->bill['income'], 'status' => 0, 'number' => 12313131]
                );
        } catch (\Exception $e) {
            $log->pushHandler(new StreamHandler(storage_path('logs/job/error.log'),Logger::ERROR) );
            $log->addError(date('Y-m-d H:i:s').' station_bills service_id = '.$this->id.' 修改失败');
        }
        $log->pushHandler(new StreamHandler(storage_path('logs/job/success.log'),Logger::INFO) );
        $log->addError(date('Y-m-d H:i:s').' station_bills service_id = '.$this->id.' 修改成功');
    }
}
